package spring.boot.fastdep.jwt.service;

import com.louislivi.fastdep.shirojwt.jwt.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import spring.boot.fastdep.jwt.common.Results;
import spring.boot.fastdep.jwt.model.TUser;
import spring.boot.fastdep.jwt.utils.StringUtils;
import spring.boot.fastdep.jwt.utils.PasswordUtils;
import spring.boot.fastdep.jwt.vo.TUserLoginVO;

@Service
public class LoginService {

    @Autowired
    private TUserService tUserService;

    @Autowired
    private JwtUtil jwtUtil;


    public Results loginUsernamePassword(TUserLoginVO vo) {
        if (vo == null || StringUtils.isNull(vo.getUsername()) || StringUtils.isNull(vo.getPassword())) {
            return Results.paramError("登录参数错误");
        }
        TUser tUser = tUserService.selectUserByUsername(vo.getUsername());
        if (tUser != null && tUser.getPassword() != null && tUser.getPassword().equals(PasswordUtils.md5Pass(vo.getPassword()))) {
            Object token = jwtUtil.sign(String.valueOf(tUser.getId()));
            return Results.success(token);
        } else {
            return Results.paramError("登录参数错误");
        }
    }

}
